﻿using System;
using System.ServiceModel;
using FileManager_Client.BusinessLogic.FileManagerUserService;
using FileManager_Client.Utility;

namespace FileManager_Client.BusinessLogic
{
    public class UserService
    {
        #region  登录事件
        public event LoginEventDelegate OnCompletedEvent;
        public delegate void LoginEventDelegate(object sender, LoginEventArgs e);
        private delegate void LoginDelegate(UserLoginArgs userLoginArgs); 
        public void LoginService(UserLoginArgs userLoginArgs)
        {
            var loginDelegate = new LoginDelegate(AsyncLoginService);
            loginDelegate.BeginInvoke(userLoginArgs, null, null);
        }
        private void AsyncLoginService(UserLoginArgs userLoginArgs)
        {
            try
            {
                var wcfService =
                    CacheGlobal.GetCache(GlobalString.System_Config_String.Wcf_FileServiceCacheKey) as FileManager_UserServiceClient;
                var loginEventArgs = new LoginEventArgs();
                if (null == wcfService || null == OnCompletedEvent)
                {
                    loginEventArgs.Error = GlobalString.System_Exception_String.RemoteServiceError;
                    loginEventArgs.Result = null;
                    return;
                }
                var result = wcfService.Login(userLoginArgs);
                loginEventArgs.Result = result ;
                OnCompletedEvent.Invoke(this, loginEventArgs);
            }
            catch (Exception ex)
            {
                var loginEventArgs = new LoginEventArgs
                                         {
                                             Error = GlobalString.System_Exception_String.RemoteServiceError,
                                             Result = null
                                         };
                LogLoader.Instance.Log.Error("AsyncLoginService has problem",ex);
                OnCompletedEvent.Invoke(this, loginEventArgs);
            }

        }
        public LoginEventArgs SyncLoginService(UserLoginArgs userLoginArgs)
        {
            var loginEventArgs = new LoginEventArgs();
            try
            {
                var wcfService =
                    CacheGlobal.GetCache(GlobalString.System_Config_String.Wcf_FileServiceCacheKey) as FileManager_UserServiceClient;
                if (null == wcfService)
                {
                    loginEventArgs.Error = GlobalString.System_Exception_String.RemoteServiceError;
                    loginEventArgs.Result = null;
                    return loginEventArgs;
                }
                if(wcfService.State != CommunicationState.Opened)
                    wcfService.Open();
                var result = wcfService.Login(userLoginArgs);
                loginEventArgs.Result = result;
            }
            catch (Exception ex)
            {
                CacheGlobal.Add(GlobalString.System_Config_String.Wcf_FileServiceCacheKey,new FileManager_UserServiceClient());
                loginEventArgs.Error = GlobalString.System_Exception_String.RemoteServiceError;
                loginEventArgs.Result = null;
                LogLoader.Instance.Log.Error("SyncLoginService has problem", ex);
            }
            return loginEventArgs;
        }
        #endregion

        #region 获取部门信息事件
        public event GetCompanyEventDelegate OnGetComapnyCollectionComplatedEvent;

        public delegate void GetCompanyEventDelegate(object sender, GetComapnyEventArgs e);

        private delegate void GetCompanyDelegate();
        public void GetCompanyService()
        {
            var getcompanyDelegate = new GetCompanyDelegate(AsyncGetCompanyService);
            getcompanyDelegate.BeginInvoke(null,null);
        }
        private void AsyncGetCompanyService()
        {
            try
            {
                var wcfService =
                    CacheGlobal.GetCache(GlobalString.System_Config_String.Wcf_FileServiceCacheKey) as FileManager_UserServiceClient;
                var getcompanyEventArgs = new GetComapnyEventArgs();
                if (null == wcfService || null == OnGetComapnyCollectionComplatedEvent)
                {
                    getcompanyEventArgs.Error = GlobalString.System_Exception_String.RemoteServiceError;
                    getcompanyEventArgs.Result = null;
                    return;
                }
                var result = wcfService.GetCompanyCollection();
                getcompanyEventArgs.Result = result;
                OnGetComapnyCollectionComplatedEvent.Invoke(this, getcompanyEventArgs);
            }
            catch (Exception ex)
            {
                var getcompanyEventArgs = new GetComapnyEventArgs
                {
                    Error = GlobalString.System_Exception_String.RemoteServiceError,
                    Result = null
                };
                LogLoader.Instance.Log.Error("AsyncGetCompanyService has problem", ex);
                OnGetComapnyCollectionComplatedEvent.Invoke(this, getcompanyEventArgs);
            }
        }

        public GetComapnyEventArgs SyncGetCompanyService()
        {
            var getcompanyEventArgs = new GetComapnyEventArgs();
            try
            {
                var wcfService =
                    CacheGlobal.GetCache(GlobalString.System_Config_String.Wcf_FileServiceCacheKey) as FileManager_UserServiceClient;
                if (null == wcfService)
                {
                    getcompanyEventArgs.Error = GlobalString.System_Exception_String.RemoteServiceError;
                    getcompanyEventArgs.Result = null;
                    return getcompanyEventArgs;
                }
                if (wcfService.State != CommunicationState.Opened)
                    wcfService.Open();
                var result = wcfService.GetCompanyCollection();
                getcompanyEventArgs.Result = result;
            }
            catch (Exception ex)
            {
                getcompanyEventArgs.Error = GlobalString.System_Exception_String.RemoteServiceError;
                getcompanyEventArgs.Result = null;
                LogLoader.Instance.Log.Error("SyncGetCompanyService has problem", ex);
            }
            return getcompanyEventArgs;
        }
        #endregion

        #region 获取文件夹集合事件
        public event GetFolderEventDelegate OnGetFolderCollectionComplatedEvent;

        public delegate void GetFolderEventDelegate(object sender, GetFolderEventArgs e);

        private delegate void GetFolderDelegate();
        public void GetFolderService()
        {
            var getFolderDelegate = new GetFolderDelegate(AsyncGetFolderService);
            getFolderDelegate.BeginInvoke(null, null);
        }
        private void AsyncGetFolderService()
        {
            try
            {
                var wcfService =
                    CacheGlobal.GetCache(GlobalString.System_Config_String.Wcf_FileServiceCacheKey) as FileManager_UserServiceClient;
                var getFolderEventArgs = new GetFolderEventArgs();
                if (null == wcfService || null == OnGetFolderCollectionComplatedEvent)
                {
                    getFolderEventArgs.Error = GlobalString.System_Exception_String.RemoteServiceError;
                    getFolderEventArgs.Result = null;
                    return;
                }
                var result = wcfService.GetDepartmentFolder();
                getFolderEventArgs.Result = result;
                OnGetFolderCollectionComplatedEvent.Invoke(this, getFolderEventArgs);
            }
            catch (Exception ex)
            {
                var getFolderEventArgs = new GetFolderEventArgs
                {
                    Error = GlobalString.System_Exception_String.RemoteServiceError,
                    Result = null
                };
                LogLoader.Instance.Log.Error("AsyncGetFolderService has problem", ex);
                OnGetFolderCollectionComplatedEvent.Invoke(this, getFolderEventArgs);
            }
        }

        public GetFolderEventArgs SyncGetFolderService()
        {
            var getFolderEventArgs = new GetFolderEventArgs();
            try
            {
                var wcfService =
                    CacheGlobal.GetCache(GlobalString.System_Config_String.Wcf_FileServiceCacheKey) as FileManager_UserServiceClient;
                if (null == wcfService)
                {
                    getFolderEventArgs.Error = GlobalString.System_Exception_String.RemoteServiceError;
                    getFolderEventArgs.Result = null;
                    return getFolderEventArgs;
                }
                if (wcfService.State != CommunicationState.Opened)
                    wcfService.Open();
                var result = wcfService.GetDepartmentFolder();
                getFolderEventArgs.Result = result;
            }
            catch (Exception ex)
            {
                getFolderEventArgs.Error = GlobalString.System_Exception_String.RemoteServiceError;
                getFolderEventArgs.Result = null;
                LogLoader.Instance.Log.Error("SyncGetFolderService has problem", ex);
            }
            return getFolderEventArgs;
        }

        #endregion

        #region 获取文件夹权限事件
        public event GetFolderRoleEventDelegate OnGetFolderRoleCollectionComplatedEvent;

        public delegate void GetFolderRoleEventDelegate(object sender, GetFolderRoleEventArgs e);

        private delegate void GetFolderRoleDelegate();
        public void GetFolderRoleService()
        {
            var getFolderRoleDelegate = new GetFolderRoleDelegate(AsyncGetFolderRoleService);
            getFolderRoleDelegate.BeginInvoke(null, null);
        }
        private void AsyncGetFolderRoleService()
        {
            try
            {
                var wcfService =
                    CacheGlobal.GetCache(GlobalString.System_Config_String.Wcf_FileServiceCacheKey) as FileManager_UserServiceClient;
                var getFolderRoleEventArgs = new GetFolderRoleEventArgs();
                if (null == wcfService || null == OnGetFolderRoleCollectionComplatedEvent)
                {
                    getFolderRoleEventArgs.Error = GlobalString.System_Exception_String.RemoteServiceError;
                    getFolderRoleEventArgs.Result = null;
                    return;
                }
                var userSession =
                    CacheGlobal.GetCache(GlobalString.System_Config_String.UserSession) as UserLoginResult ??
                    new UserLoginResult();
                var remoteArgs = new FolderRoleQueryArgs
                                     {
                                         CompanyIdk__BackingField = userSession.CompanyIdk__BackingField,
                                         UserIdk__BackingField = userSession.UserIdk__BackingField
                                     };
                var result = wcfService.GetFolderRoleCollection(remoteArgs);
                getFolderRoleEventArgs.Result = result;
                OnGetFolderRoleCollectionComplatedEvent.Invoke(this, getFolderRoleEventArgs);
            }
            catch (Exception ex)
            {
                var getFolderRoleEventArgs = new GetFolderRoleEventArgs
                {
                    Error = GlobalString.System_Exception_String.RemoteServiceError,
                    Result = null
                };
                LogLoader.Instance.Log.Error("AsyncGetFolderRoleService has problem", ex);
                OnGetFolderRoleCollectionComplatedEvent.Invoke(this, getFolderRoleEventArgs);
            }
        }

        public GetFolderRoleEventArgs SyncGetFolderRoleService()
        {
            var getFolderRoleEventArgs = new GetFolderRoleEventArgs();
            try
            {
                var wcfService =
                    CacheGlobal.GetCache(GlobalString.System_Config_String.Wcf_FileServiceCacheKey) as FileManager_UserServiceClient;
                if (null == wcfService)
                {
                    getFolderRoleEventArgs.Error = GlobalString.System_Exception_String.RemoteServiceError;
                    getFolderRoleEventArgs.Result = null;
                    return getFolderRoleEventArgs;
                }
                var userSession =
                    CacheGlobal.GetCache(GlobalString.System_Config_String.UserSession) as UserLoginResult ??
                    new UserLoginResult();
                var remoteArgs = new FolderRoleQueryArgs
                {
                    CompanyIdk__BackingField = userSession.CompanyIdk__BackingField,
                    UserIdk__BackingField = userSession.UserIdk__BackingField
                };
                if (wcfService.State != CommunicationState.Opened)
                    wcfService.Open();
                var result = wcfService.GetFolderRoleCollection(remoteArgs);
                getFolderRoleEventArgs.Result = result;
            }
            catch (Exception ex)
            {
                getFolderRoleEventArgs.Error = GlobalString.System_Exception_String.RemoteServiceError;
                getFolderRoleEventArgs.Result = null;
                LogLoader.Instance.Log.Error("SyncGetFolderRoleService has problem", ex);
            }
            return getFolderRoleEventArgs;
        }
        #endregion

        public UserResultCollection SearchUserByName(SearchUserNameArgs searchUserNameArgs)
        {
            var result = new UserResultCollection();
            try
            {
                var wcfService =
    CacheGlobal.GetCache(GlobalString.System_Config_String.Wcf_FileServiceCacheKey) as FileManager_UserServiceClient;
                if (wcfService != null)
                    result = wcfService.SearchUserByName(searchUserNameArgs);
            }
            catch (Exception ex)
            {
                LogLoader.Instance.Log.Error("SearchUserByName has problem", ex);
            }
            return result;
        }

        public CompanGroupAndUserGroupResultCollection GetCompanyGroupAndUserGroup()
        {
            var result = new CompanGroupAndUserGroupResultCollection();
            try
            {
                var wcfService =
    CacheGlobal.GetCache(GlobalString.System_Config_String.Wcf_FileServiceCacheKey) as FileManager_UserServiceClient;
                if (wcfService != null)
                    result = wcfService.GetCompanyGroupAndUserGroupCollection();
            }
            catch (Exception ex)
            {
                LogLoader.Instance.Log.Error("GetCompanyGroupAndUserGroup has problem", ex);
            }
            return result;
        }

        public UserResultCollection SearchUserByCompanyCode(SearchUserByCompanyCodeArgs searchUserByCompanyCodeArgs)
        {
            var result = new UserResultCollection();
            try
            {
                var wcfService =
    CacheGlobal.GetCache(GlobalString.System_Config_String.Wcf_FileServiceCacheKey) as FileManager_UserServiceClient;
                if (wcfService != null)
                    result = wcfService.SearchUserByCompanyCode(searchUserByCompanyCodeArgs);
            }
            catch (Exception ex)
            {
                LogLoader.Instance.Log.Error("SearchUserByCompanyCode has problem", ex);
            }
            return result;
        }

        public UpdateFileInfo GetLastVersion()
        {
            UpdateFileInfo result = null;
            try
            {
                var wcfService =
                   CacheGlobal.GetCache(GlobalString.System_Config_String.Wcf_FileServiceCacheKey) as FileManager_UserServiceClient;
                if(null == wcfService)
                {
                    wcfService = new FileManager_UserServiceClient();
                    CacheGlobal.Add(GlobalString.System_Config_String.Wcf_FileServiceCacheKey,wcfService);
                }
                if(wcfService.State != CommunicationState.Opened)
                    wcfService.Open();
                result = wcfService.CheckClientUpdateInfo();
            }
            catch (Exception ex)
            {
                LogLoader.Instance.Log.Error("GetLastVersion has problem", ex);
                return null;
            }
            return result;
        }
    }

    public class LoginEventArgs : EventArgs
    {
        public UserLoginResult Result { get; set; }
        public string Error { get; set; }
    }

    public class GetComapnyEventArgs:EventArgs
    {
        public CompanyResultCollection Result { get; set; }
        public string Error { get; set; }
    }

    public class  GetFolderEventArgs:EventArgs
    {
        public FolderResultCollection Result { get; set; }
        public string Error { get; set; }
    }

    public class GetFolderRoleEventArgs
    {
        public FolderRoleResultCollection Result { get; set; }
        public string Error { get; set; }
    }
}